package com.zzr.demo.redis.controller;

import com.zzr.demo.redis.service.SocialRelationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Set;

/**
 * @author: xiaomi
 * @date: 2021/10/8
 * @description:
 */
@RestController
@RequestMapping("social/relationship")
@Api(tags = "社交关系：查看正在关注及关注者信息")
@Slf4j
public class SocialRelationController {

    @Autowired
    SocialRelationService socialRelationService;

    @GetMapping("/follow/{follower}/{master}")
    @ApiOperation("关注")
    public boolean follow(@PathVariable("follower") String follower, @PathVariable("master") String master) {
        socialRelationService.follow(follower, master);
        return true;
    }

    @GetMapping("/unfollow/{follower}/{master}")
    @ApiOperation("取消关注")
    public boolean unfollow(@PathVariable("follower") String follower, @PathVariable("master") String master) {
        socialRelationService.unfollow(follower, master);
        return true;
    }

    @GetMapping("/getFollowersCount/{userId}")
    @ApiOperation("获取用户的关注者数量")
    public long getFollowersCount(@PathVariable("userId") String userId) {
        long followerCount = socialRelationService.getFollowerCount(userId);
        return followerCount;
    }

    @GetMapping("/getFollowers/{userId}")
    @ApiOperation("获取用户的关注者集合")
    public Set<String> getFollowers(@PathVariable("userId") String userId) {
        Set<String> followers = socialRelationService.getFollowers(userId);
        return followers;
    }

    @GetMapping("/getFollowingCount/{userId}")
    @ApiOperation("获取用户的正在关注数量")
    public long getFollowingCount(@PathVariable("userId") String userId) {
        long followingCount = socialRelationService.getFollowingCount(userId);
        return followingCount;
    }

    @GetMapping("/getFollowings/{userId}")
    @ApiOperation("获取用户的正在关注集合")
    public Set<String> getFollowings(@PathVariable("userId") String userId) {
        Set<String> followings = socialRelationService.getFollowings(userId);
        return followings;
    }

    /**
     * 192.168.56.10:6379> smembers social::following::1
     * 1) "2"
     * 2) "mary"
     * 192.168.56.10:6379> smembers social::following::peter
     * 1) "2"
     * 2) "tom"
     * 3) "jack"
     *
     * @param userId1
     * @param userId2
     * @return
     */
    @GetMapping("/getCommonFollowings/{userId1}/{userId2}")
    @ApiOperation("获取两个用户的共同关注集合")
    public Set<String> getCommonFollowings(@PathVariable("userId1") String userId1, @PathVariable("userId2") String userId2) {
        Set<String> commonFollowings = socialRelationService.getCommonFollowings(userId1, userId2);
        return commonFollowings;
    }
}
